<template>
  <el-dialog
    title="选择角色"
    :visible.sync="roleVisible"
    width="60%"
    append-to-body
    :before-close="handleClose"
  >
    <div class="dialog_content">
      <div class="dialog_left">
        <el-input
          placeholder="请输入角色名称"
          v-model="searchVal"
          class="input-with-select"
        >
          <el-button
            @click="searchRole"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
        <div
          @click="addRole(item, index)"
          :class="item.isChecked ? 'role_item active' : 'role_item'"
          v-for="(item, index) in roleList"
          :key="index"
        >
          <div>
            {{ item.roleName }}
          </div>
          <div v-show="item.isChecked">
            <i class="el-icon-check"></i>
          </div>
        </div>
      </div>
      <div class="dialog_right">
        <div class="right_title">
          <div>
            已选
            <span style="color: #79bbff; font-weight: bold">{{ total }}</span>
            项
          </div>
          <div @click="clearAll" style="color: #f67c7c">清空</div>
        </div>
        <div
          class="role_tag"
          v-for="(item, index) in showRoleList"
          :key="index"
        >
          <el-tag @close="handleCloseTag(item, index)" closable>
            {{ item.roleName }}
          </el-tag>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="commit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listRole } from "@/api/system/role";
export default {
  props: ["visible"],
  data() {
    return {
      searchVal: "",
      roleVisible: false,
      showRoleList: [],
      roleList: [],
    };
  },
  computed: {
    total() {
      return this.showRoleList.length;
    },
  },
  watch: {
    visible(state) {
      this.roleVisible = state;
      if (state) {
        this.listRoleApi();
        this.showRoleList = [...this.$store.state.workflow.roleList];
      } else {
        this.roleList = [];
      }
    },
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.$emit("updateRoleList", this.showRoleList);
          this.$emit("update:visible", false);
          done();
        })
        .catch((_) => {});
      //TODO
    },
    handleCloseTag(item, index) {
      const i = this.showRoleList.findIndex((e) => e.roleId === item.roleId);
      this.$set(this.roleList[i], "isChecked", false);
      this.showRoleList.splice(index, 1);
    },
    addRole(item, index) {
      if (this.showRoleList.includes(item)) {
        this.$set(this.roleList[index], "isChecked", false);
        const i = this.showRoleList.findIndex((e) => e.roleId === item.roleId);
        this.showRoleList.splice(i, 1);
      } else {
        this.showRoleList.push(item);
        this.$set(this.roleList[index], "isChecked", true);
      }
    },
    //搜索
    searchRole() {
      this.roleList = this.roleList.filter((e) =>
        e.roleName.includes(this.searchVal)
      );
    },
    clearAll() {
      this.showRoleList = [];
    },
    commit() {
      this.$emit("updateRoleList", this.showRoleList);
      this.$emit("update:visible", false);
    },
    listRoleApi() {
      listRole().then((res) => {
        if (res.code === 200) {
          this.roleList = res.rows
            .filter((e) => e.roleId !== "1")
            .map((item) => {
              return {
                ...item,
                isChecked: false,
              };
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog_content {
  height: 300px;
  display: flex;
  justify-content: space-around;
  .dialog_left,
  .dialog_right {
    border: 1px solid #ccc;
    width: 45%;
    height: 100%;
    padding: 10px;
    overflow: auto;
  }
  .right_title {
    display: flex;
    justify-content: space-between;
  }
  .role_tag {
    margin-top: 10px;
  }
  .role_item {
    display: flex;
    justify-content: space-between;
    border-radius: 5px;
    padding: 10px;
    margin-top: 5px;
    background-color: #f5f7fa;
    cursor: pointer;
  }
  .active {
    color: #1890ff;
  }
  .role_item:last-child {
    border-bottom: none;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .el-tag {
    cursor: pointer;
  }
}
</style>
